package com.tb.service;

import com.tb.mapper.FollowMapper;
import com.tb.model.Follow;
import com.tb.model.FollowExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: wangguichun
 * @create: 2018-04-13 22:47
 **/

@Service
public class FollowService {

    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UserService userService;

    public void addRecord(Follow follow){
        followMapper.insertSelective(follow);
    }


    public List<Follow> getListByFormUserId(int fromUserId) {
        FollowExample example = new FollowExample();
        FollowExample.Criteria criteria = example.createCriteria();
        criteria.andFromIdEqualTo(fromUserId);
        List<Follow> followList = followMapper.selectByExample(example);
        for (Follow follow : followList) {
            follow.setFromUser(userService.getUserById(fromUserId));
            follow.setToUser(userService.getUserById(follow.getToId()));
        }
        return followList;
    }

    /**
     * 查看有没有关注记录
     * @param fromId 关注着
     * @param toId  被关注者
     * @return
     */
    public boolean isFollow(int fromId, Integer toId) {
        FollowExample example = new FollowExample();
        FollowExample.Criteria criteria = example.createCriteria();
        criteria.andFromIdEqualTo(fromId);
        criteria.andToIdEqualTo(toId);
        List<Follow> followList = followMapper.selectByExample(example);

        return !followList.isEmpty();
    }

    public void addFollowRecord(Follow follow) {
        followMapper.insertSelective(follow);
    }

    public void removeFollowRecord(Follow follow) {
        FollowExample example = new FollowExample();
        FollowExample.Criteria criteria = example.createCriteria();
        criteria.andFromIdEqualTo(follow.getFromId());
        criteria.andToIdEqualTo(follow.getToId());
        followMapper.deleteByExample(example);
    }
}
